{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 引包\n",
    "from sklearn import datasets, cross_validation, naive_bayes\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据加载方法 加载sklearn自带的数字识别数据：\n",
    "def load_data():\n",
    "    digits=datasets.load_digits()\n",
    "    return cross_validation.train_test_split(digits.data, digits.target, test_size=0.25, random_state=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 高斯贝叶斯  条件概率分布满足正态分布：\n",
    "def test_GaussianNB(*data):\n",
    "    train_x,test_x, train_y, test_y = data\n",
    "    cls = naive_bayes.GaussianNB()\n",
    "    cls.fit(train_x,train_y)\n",
    "    print('Training Score: {0}'.format(cls.score(train_x,train_y)))\n",
    "    print('Testing Score: {0}'.format(cls.score(test_x,test_y)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training Score: 0.8574610244988864\n",
      "Testing Score: 0.8333333333333334\n"
     ]
    }
   ],
   "source": [
    "# 查看高斯贝叶斯训练结果：\n",
    "train_x,test_x, train_y, test_y = load_data()\n",
    "test_GaussianNB(train_x, test_x, train_y, test_y)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
